package com.wu.controller;

import com.aliyuncs.utils.StringUtils;
import com.wu.error.BusinessException;
import com.wu.error.EnumBusinessError;
import com.wu.response.CommonReturnType;
import com.wu.service.UsersService;
import com.wu.util.FTPUtil;
import com.wu.util.MyUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

/**
 * @author: 吴磊
 * @program: blog
 * @create: 2021-04-20 18:51
 */
@RestController
@Api(tags = "用户接口")
public class UsersController {
    @Autowired
    private UsersService usersService;

    @Autowired
    private FTPUtil ftpUtil;

    /**
     * 判断手机号是否已经存在
     *
     * @param telPhone
     * @return
     */
    @ApiOperation(value = "判断手机号是否已经存在")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "telPhone", value = "手机号", required = true, dataTypeClass = String.class)
    })
    @RequestMapping(value = "/checkPhone", method = RequestMethod.POST)
    public CommonReturnType checkPhone(@RequestParam("telPhone") String telPhone) {
        usersService.telPhoneIsOk(telPhone);
        return CommonReturnType.create(null);
    }

    /**
     * 更新用户头像
     *
     * @param file
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "更新用户头像")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "头像图片", required = true, dataTypeClass = MultipartFile.class)
    })
    @RequestMapping("/updateUserPhoto")
    public CommonReturnType tokenUpdateUserPhoto(MultipartFile file) throws Exception {
        String filePath = null;
        if (file != null && !file.isEmpty()) {
            filePath = ftpUtil.uploadFolder(file);
        }
        usersService.updateUserInfo(null, null, null, filePath, null, null, null, null);
        return CommonReturnType.create(null);
    }

    /**
     * 更新用户数据
     *
     * @param userName
     * @param password
     * @param userEmail
     * @param file
     * @param telPhone
     * @param userNickname
     * @param userSex
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "更新用户数据，传什么更新什么")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userName", value = "用户真实名称", required = false, dataTypeClass = String.class),
            @ApiImplicitParam(name = "password", value = "密码", required = false, dataTypeClass = String.class),
            @ApiImplicitParam(name = "userEmail", value = "用户邮箱", required = false, dataTypeClass = String.class),
            @ApiImplicitParam(name = "file", value = "头像图片", required = false, dataTypeClass = MultipartFile.class),
            @ApiImplicitParam(name = "telPhone", value = "电话号码", required = false, dataTypeClass = String.class),
            @ApiImplicitParam(name = "userNickname", value = "用户昵称 0 男 1女", required = false, dataTypeClass = String.class),
            @ApiImplicitParam(name = "userSex", value = "用户性别", required = false, dataTypeClass = String.class),
            @ApiImplicitParam(name = "userIntroduce", value = "用户介绍", required = false, dataTypeClass = String.class),
    })
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public CommonReturnType tokenUpdateUserInfo(@RequestParam(name = "userName", required = false) String userName,
                                                @RequestParam(name = "password", required = false) String password,
                                                @RequestParam(name = "userEmail", required = false) String userEmail,
                                                @RequestParam(name = "file", required = false) MultipartFile file,
                                                @RequestParam(name = "telPhone", required = false) String telPhone,
                                                @RequestParam(name = "userNickname", required = false) String userNickname,
                                                @RequestParam(name = "userSex", required = false) String userSex,
                                                @RequestParam(name = "userIntroduce", required = false) String userIntroduce
    ) throws Exception {
        String filePath = null;
        if (file != null && !file.isEmpty()) {
            filePath = ftpUtil.uploadFolder(file);
        }
        usersService.updateUserInfo(userName, password, userEmail, filePath, telPhone, userNickname, userSex, userIntroduce);
        return CommonReturnType.create(null);
    }

    /**
     * 验证用户的密码
     *
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "getPassword", method = RequestMethod.POST)
    public CommonReturnType tokenGetUserPassword(@RequestParam("password") String password) throws Exception {
        if (StringUtils.isEmpty(password)) {
            throw new BusinessException(EnumBusinessError.PARAMETER_INVALID_ERROR, "用户密码不合法");
        }
        return CommonReturnType.create(usersService.getUserPassword().equals(MyUtil.MD5(password)));
    }

    /**
     * 返回用户信息
     *
     * @return
     */
    @RequestMapping(value = "/getUserInfo", method = RequestMethod.POST)
    public CommonReturnType getUserInfo(@RequestParam(name = "userId", required = false) String userId) {
        Map<String, Object> userInfo = usersService.getUserInfo(userId);
        return CommonReturnType.create(userInfo);
    }


    /**
     * 查询用户主页面的主要信息
     *
     * @param userId
     * @return
     */
    @RequestMapping(value = "/getUserMainInfo", method = RequestMethod.POST)
    public CommonReturnType getUserMainInfo(@RequestParam("userId") String userId) {
        Map<String, Object> userMainInfo = usersService.getUserMainInfo(userId);
        return CommonReturnType.create(userMainInfo);
    }

    /**
     * 是否是用户对外开放的主界面
     *
     * @param userId
     * @return
     */
    @RequestMapping(value = "/isOwnerMainUI", method = RequestMethod.POST)
    public CommonReturnType isOwnerMainUI(@RequestParam("userId") String userId) {
        Map<String, Object> map = usersService.isOwnerMainUI(userId);
        return CommonReturnType.create(map);
    }

    /**
     * 获取当前登录的用户文章下未审核过的评论数量
     *
     * @return
     */
    @RequestMapping(value = "/getUserArticleCommentsWithNotPass", method = RequestMethod.POST)
    public CommonReturnType getUserArticleCommentsWithNotPass() {
        return CommonReturnType.create(usersService.getUserArticleCommentsWithNotPass());
    }

}
